<?php
namespace Controller;
use Kuyuan\WqCore\controller\Controller;
use Model\DepositModel;

/**
 * 提现
 * Class Deposit
 * @package Controller
 */
class Deposit extends Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 创建提现
     * @param array $_data
     * @return bool|string 返回提现订单id
     */
    public function add(Array $_data)
    {
        $_data["order_id"] = $this->createOrderId();
        $_data["uid"] = $this->_uid;
        $_data["uniacid"] = $this->_uniacid;
        if(DepositModel::create($_data)) {
            return $_data["order_id"];
        } else {
            return false;
        }
    }

    /**
     * 创建订单id
     */
    private function createOrderId()
    {
        $_order_id = TIMESTAMP.mt_rand(10000,99999);
        if(DepositModel::where("order_id",$_order_id)->value("order_id")) {
            return $this->createOrderId();
        }
        return $_order_id;
    }

    /**
     * 修改提现信息
     * @param array $_data
     * @param $_order_id
     * @return mixed
     */
    public function edit(Array $_data,$_order_id)
    {
        return DepositModel::where("order_id",$_order_id)->update($_data);
    }

    /**
     * 获取提现列表
     * @param array $_limit
     * @param int $_uid 用户id
     * @return mixed
     */
    public function getUserList(Array $_limit,$_uid = 0)
    {
        if(!$_uid) $_uid = $this->_uid;
        return DepositModel::where("uid",$_uid)
            ->where("uniacid",$this->_uniacid)
            ->where("status","<",9)
            ->field(array("id","status","money","time"))
            ->limit($_limit[0],$_limit[1])
            ->order("time","desc")
            ->noGetAttr("user")
            ->select();
    }

    /**
     * 获取提现列表
     * @param array $_limit
     * @param $_status
     * @return DepositModel
     */
    public function getList(Array $_limit,$_status)
    {
        return DepositModel::where("uniacid",$this->_uniacid)
            ->where("status",$_status)
            ->field(array("id","uid","money","status","time"))
            ->order("time","desc")
            ->limit($_limit[0],$_limit[1])
            ->select();
    }

    /**
     * 获取数据总数
     * @param $_status
     * @return mixed
     */
    public function getTotal($_status)
    {
        return DepositModel::where("uniacid",$this->_uniacid)
            ->where("status",$_status)
            ->count();
    }

    /**
     * 通过提现id返回提现信息
     * @param int $_deposit_id 提现id
     * @return mixed
     */
    public function find($_deposit_id)
    {
        return DepositModel::where("id",$_deposit_id)
            ->field(array("money","uid","order_id","status"))
            ->find();
    }
}